package com.quick.develop.form.controller;

import cn.hutool.core.collection.CollUtil;
import com.quick.develop.framework.common.page.PageResult;
import com.quick.develop.framework.common.utils.Result;
import com.quick.develop.form.entity.GenDynamicFormFieldEntity;
import com.quick.develop.form.query.GenDynamicFormFieldQuery;
import com.quick.develop.form.vo.FieldSortVO;
import com.quick.develop.form.vo.FormFieldBatchVO;
import com.quick.develop.form.vo.GenDynamicFormFieldVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import com.quick.develop.form.convert.GenDynamicFormFieldConvert;
import com.quick.develop.form.service.GenDynamicFormFieldService;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 表单字段
 *
 * @author Tioo 1906559468@qq.com
 * @since 1.0.0 2023-09-24
 */
@RestController
@RequestMapping("maku-generator/gen_dynamic_form_field")
@Tag(name = "表单字段")
@AllArgsConstructor
public class GenDynamicFormFieldController {
    private final GenDynamicFormFieldService genDynamicFormFieldService;

    @GetMapping("page")
    @Operation(summary = "分页")
    public Result<PageResult<GenDynamicFormFieldVO>> page(@Valid GenDynamicFormFieldQuery query) {
        PageResult<GenDynamicFormFieldVO> page = genDynamicFormFieldService.page(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<GenDynamicFormFieldVO> get(@PathVariable("id") Long id) {
        GenDynamicFormFieldEntity entity = genDynamicFormFieldService.getById(id);

        return Result.ok(GenDynamicFormFieldConvert.INSTANCE.convert(entity));
    }

    @PostMapping
    @Operation(summary = "保存")
    public Result<String> save(@RequestBody GenDynamicFormFieldVO vo) {
        genDynamicFormFieldService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    public Result<String> update(@RequestBody @Valid GenDynamicFormFieldVO vo) {
        genDynamicFormFieldService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    public Result<String> delete(@RequestBody List<Long> idList) {
        genDynamicFormFieldService.delete(idList);

        return Result.ok();
    }

    @DeleteMapping(value = "{id}")
    @Operation(summary = "删除")
    public Result<String> delete(@PathVariable("id") Long id) {
        genDynamicFormFieldService.removeById(id);

        return Result.ok();
    }

    @PostMapping(value = "batch")
    @Operation(summary = "批量保存")
    public Result<?> batchSave(@RequestBody FormFieldBatchVO batchVO) {
        genDynamicFormFieldService.batchSave(batchVO);
        return Result.ok();
    }

    @PutMapping(value = "batch")
    @Operation(summary = "批量保存")
    public Result<?> batchUpdate(@RequestBody FormFieldBatchVO batchVO) {
        genDynamicFormFieldService.batchUpdate(batchVO);
        return Result.ok();
    }

    @PostMapping(value = "online")
    public Result<?> online(@RequestBody FormFieldBatchVO batchVO) {
        if (CollUtil.isNotEmpty(batchVO.getFields())) {
            genDynamicFormFieldService.updateBatchById(batchVO.getFields());
        }
        return Result.ok();
    }

    @PostMapping(value = "exchange/order")
    public Result<?> exchangeOrder(@RequestBody FieldSortVO fieldSortVO) {
        genDynamicFormFieldService.exchangeOrder(fieldSortVO);
        return Result.ok();
    }
}